<template>
	<view>
		<view class="feedback-title">意见反馈</view>
		<view class="feedback-content">
			<textarea v-model="content" placeholder="请输入您要反馈的问题" />
		</view>
		<view class="feedback-title">反馈图片</view>
		<view class="feedback-img-box">
			<view class="feedback-img-item" v-for="(item,index) in images" :key="index">
				<view class="close-icon" @click="delImage(index)">
					<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				</view>
				<view class="feedback-img">
					<image :src="item.filePath" mode="aspectFill"></image>
				</view>
			</view>
			<view v-if="images.length < 6" class="feedback-img-item" @click="addImage">
				<view class="feedback-img">
					<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
				</view>
			</view>
		</view>
		<button class="feedback-btn" type="primary" @click="submit">提交反馈意见</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				images: [],
				content: ''
			}
		},
		methods: {
			addFeedback({feedbackImages,content}){
				this.$api.addFeedback({feedbackImages,content}).then(res=>{
					uni.hideLoading()
					uni.showToast({
						title:'反馈提交成功'
					})
					setTimeout(()=>{
						uni.switchTab({
							url:'/pages/tabbar/my/my'
						})
					},2000)
				}).catch(err=>{
					uni.hideLoading()
					uni.showToast({
						title:'反馈提交失败',
						icon:'none'
					})
				})
			},
			// 同步图片上传
			async uploadImage(filePath, fileName) {
				const result = await uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: fileName
				})
				return result.fileID
			},
			async submit() {
				if (!this.content) {
					uni.showToast({
						title: '请输入反馈意见',
						icon: 'none'
					})
					return
				}

				let imagePaths = []
				uni.showLoading({
					title: "正在上传,请稍候"
				})
				for (let i = 0; i < this.images.length; i++) {
					const filePath = this.images[i].filePath
					const fileName = this.images[i].fileName
					filePath = await this.uploadImage(filePath, fileName)
					imagePaths.push(filePath)
				}
				
				this.addFeedback({
					content:this.content,
					feedbackImages:imagePaths
				})
			},
			delImage(index) {
				this.images.splice(index, 1)
			},
			addImage() {
				// 最多上传6张
				let count = 6 - this.images.length
				uni.chooseImage({
					count: count,
					success: (res) => {
						const tempFilePaths = res.tempFilePaths
						const tempFiles = res.tempFiles
						// tempFilePaths.forEach((item, index) => {
						// 	// 处理h5图片数量
						// 	if (index < count) {
						// 		this.images.push({
						// 			url: item
						// 		})
						// 	}
						// })
						
						for (let i = 0; i < tempFilePaths.length; i++) {
							// 处理h5图片数量
							if (i < count) {
								this.images.push({
									filePath: tempFilePaths[i],
									fileName: tempFiles[i].name
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.feedback-title {
		margin: 25px;
		margin-bottom: 0;
		font-size: 14px;
		color: #666;
	}

	.feedback-content {
		margin: 15px;
		padding: 10px;
		border: 1px solid #eee;
		border-radius: 5px;

		textarea {
			font-size: 12px;
			width: 100%;
			height: 100px;
		}
	}

	.feedback-img-box {
		display: flex;
		flex-wrap: wrap;
		padding: 10px;

		.feedback-img-item {
			position: relative;
			width: 33.33%;
			height: 0;
			padding-top: 33.33%;
			box-sizing: border-box;

			.close-icon {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 0;
				right: 0;
				width: 22px;
				height: 22px;
				background-color: #ff5a5f;
				border-radius: 50%;
				z-index: 2;
			}

			.feedback-img {
				border: 1px solid #F5F5F5;
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 5px;
				left: 5px;
				right: 5px;
				bottom: 5px;
				border-radius: 5px;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.feedback-btn {
		margin: 0 15px;
		background-color: $mk-base-color;
	}
</style>
